#ifndef SANDBOX_LOADER
#define SANDBOX_LOADER
#include "../basic.h"
#include "../command/commands.h"
#include <map>
static std::map<byte, func> commands;
static std::pair<byte, qword> values;
void generate()
{
	commands[0x00] = nop;
	commands[0x01] = ret;
	commands[0x10] = precall;
	commands[0x11] = pushaf;
	commands[0x12] = pushad;
	commands[0x13] = pushaq;
	commands[0x19] = popaf;
	commands[0x1a] = popad;
	commands[0x1b] = popaq;
	commands[0x20] = trap;
	commands[0x21] = setb;
	commands[0x22] = clrb;
	commands[0x28] = push;
	commands[0x29] = pop;
	commands[0x2a] = pushf;
	commands[0x2b] = popf;
	commands[0x2c] = pushd;
	commands[0x2d] = popd;
	commands[0x2f] = call;
	commands[0x30] = lsh;
	commands[0x31] = rsh;
	commands[0x32] = inv;
	commands[0x33] = movra;
	commands[0x34] = movar;
	commands[0x35] = movrs;
	commands[0x36] = movsr;
	commands[0x37] = movaa;
	commands[0x38] = movrr;
	commands[0x3a] = inc;
	commands[0x3b] = dec;
	commands[0x3d] = cbw;
	commands[0x3e] = cwd;
	commands[0x3f] = cdq;
	commands[0x40] = calr;
	commands[0x41] = logr;
	commands[0x42] = calf;
	commands[0x43] = cald;
	commands[0x50] = ldr;
	commands[0x58] = str;
	commands[0x60] = calld;
	commands[0x6f] = intd;
	commands[0x80] = cali;
	commands[0x81] = logi;
	commands[0x90] = ldi;
	commands[0x98] = sti;
	commands[0xa0] = jmp;
	commands[0xa1] = br;
}
void loader()
{
	values.first = *(ram + addr_regs[Code]);
	if (values.first < 0x20)
	{
		values.second = values.first;
		addr_regs[Code] += 1;
	}
	else if (values.first < 0x40)
	{
		values.second = *(word *)(ram + addr_regs[Code]);
		addr_regs[Code] += 2;
		values.second = swapEndian(values.second);
		values.second >>= 48;
	}
	else if (values.first < 0x80)
	{
		values.second = *(dword *)(ram + addr_regs[Code]);
		addr_regs[Code] += 4;
		values.second = swapEndian(values.second);
		values.second >>= 32;
	}
	else
	{
		values.second = *(qword *)(ram + addr_regs[Code]);
		addr_regs[Code] += 8;
		values.second = swapEndian(values.second);
	}
}
void selector()
{
	std::map<byte, func>::iterator pointer = commands.find(values.first);
	try
	{
		auto function = pointer->second;
		function(values.second);
	}
	catch (std::exception e)
	{
		// TODO: Throw Error:Command not found;
	}
}
#endif // !SANDBOX_LOADER
